//
//  BannerCollectionViewCell.swift
//  iPhone
//
//  Created by Toj on 5/24/23.
//

import UIKit
import GoogleMobileAds

class BannerCollectionViewCell: UICollectionViewCell {
    
    private weak var _delegate: UICollectionViewCellDelegate?
    
    var cellModel: BannerAdCellModel? { didSet {
        guard let model = cellModel else { return }
        
        numberLabel.text = model.number
        
        guard model.isNeedLayout else {
            _adView?.removeFromSuperview()
            cellModel?.resetCellSize()
            
            loadAdView(model: model)
            return
        }
        model.isNeedLayout = false
        setupAddAdView()
    } }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupMakeAddSubviews()
        setupMakeInitialize()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        setupMakeLayoutSubviews()
    }
    
    override func prepareForReuse() {
        super.prepareForReuse()
        print("-->toj: func: prepareForReuse BannerAdCell")
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /* numberLabel */
    private lazy var numberLabel: UILabel = {
        let view = UILabel()
        view.textColor = .black
        return view
    }()
    
    /* adContentView */
    private lazy var adContentView = UIView()
    /* adView */
    private var _adView: UIView?
}

extension BannerCollectionViewCell {
    
    func loadAdView(model: BannerAdCellModel) {
        let adSize = GADAdSizeFromCGSize(model.willShowCellSize)
        let adView = GADBannerView(adSize: adSize)
        adView.adUnitID = model.adUnitID
        adView.rootViewController = UIViewController.vc_currentNavigationTopViewController
        adView.delegate = self
        
        let adRequest = GADRequest()
        adView.load(adRequest)
        _adView = adView
        //adContentView.addSubview(adView)
    }
}

extension BannerCollectionViewCell: UICollectionViewCellDataSource {
    
    var delegate: UICollectionViewCellDelegate? {
        get {
            return _delegate
        }
        set {
            _delegate = newValue
        }
    }
    
    var dataModel: UICollectionViewCellModelProtocol? {
        get {
            return cellModel
        }
        set {
            cellModel = newValue as? BannerAdCellModel
        }
    }
}

extension BannerCollectionViewCell: GADBannerViewDelegate {
    func bannerViewDidReceiveAd(_ bannerView: GADBannerView) {
        print("-->toj: func: \(#function)")
        cellModel?.setupCellSize()
        cellModel?.isNeedLayout = true
        delegate?.collectionViewReloadCell(self)
    }
    
    func bannerView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: Error) {
        
        print("-->toj: func: \(#function)")
    }
    
    func bannerViewDidRecordImpression(_ bannerView: GADBannerView) {
        
        print("-->toj: func: \(#function)")
    }
    
    func bannerViewDidRecordClick(_ bannerView: GADBannerView) {
        
        print("-->toj: func: \(#function)")
    }
    func bannerViewWillPresentScreen(_ bannerView: GADBannerView) {
        print("-->toj: func: \(#function)")
        
    }
    func bannerViewWillDismissScreen(_ bannerView: GADBannerView) {
        
        print("-->toj: func: \(#function)")
    }
    func bannerViewDidDismissScreen(_ bannerView: GADBannerView) {
        
        print("-->toj: func: \(#function)")
    }
}

extension BannerCollectionViewCell {
    
    private func setupAddAdView() {
        guard let adView = _adView else { return }
        adView.backgroundColor = .random
        adContentView.addSubview(adView)
        adView.snp.makeConstraints { make in
            make.edges.equalTo(contentView)
        }
    }
    
    private func setupMakeAddSubviews() {
        contentView.addSubview(adContentView)
        contentView.addSubview(numberLabel)
    }
    
    private func setupMakeLayoutSubviews() {
        numberLabel.snp.makeConstraints { make in
            make.edges.equalTo(contentView)
        }
        
        adContentView.snp.makeConstraints { make in
            make.edges.equalTo(contentView)
        }
    }
    
    private func setupMakeInitialize() {
        contentView.backgroundColor = .random
    }
}
